package com.banmao.CarlCamp.Day09;

import com.alibaba.fastjson.JSON;

/**
 * https://leetcode.cn/problems/reverse-string/
 */
public class LC344 {

    public void reverseString(char[] s) {
        if (s.length == 0 || s.length == 1) {
            return;
        }
        // 双指针
        int left = 0, right = s.length - 1;
        // 注意，通过异或操作进行交换时，循环条件不能取到 <=，否则在异或交换时会有问题
        // 用中间值进行交换则不需要考虑
        while (left < right) {
            s[left] ^= s[right];
            s[right] ^= s[left];
            s[left] ^= s[right];
            left++;
            right--;
        }
    }

    public static void main(String[] args) {
        LC344 lc344 = new LC344();
        char[] chars = "abcde".toCharArray();
        lc344.reverseString(chars);
        System.out.println(JSON.toJSONString(chars));

        //char a = 'c';
        //char b = 'c';
        //
        //a ^= b;
        //b ^= a;
        //a ^= b;
        //
        //System.out.println(a);
        //System.out.println(b);
    }

}
